package org.wmq.organizationmanagement.controller;

import io.minio.MinioClient;
import io.minio.errors.*;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.wmq.organizationmanagement.common.Result;
import org.wmq.organizationmanagement.model.po.MemberFile;
import org.wmq.organizationmanagement.model.vo.MemberFileVO;
import org.wmq.organizationmanagement.service.MemberFileService;
import org.wmq.organizationmanagement.service.MinioService;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

@RestController
@RequestMapping("member-file")
@RequiredArgsConstructor
@Slf4j
public class MemberFileController {
    private final MemberFileService memberFileService;
    private final MinioService minioService;
    @GetMapping("/{memberId}")
    public Result<MemberFileVO> getByMemberId(@PathVariable Long memberId){
        MemberFileVO memberFileVO = this.memberFileService.getByMemberId(memberId);
        if (memberFileVO == null){
            log.error("未查询到该用户的文件信息");
            return Result.error("未查询到该用户的文件信息");
        }
        log.info("查询到该用户的文件信息：{}", memberFileVO);
        return Result.ok(memberFileVO);
    }
    @PostMapping("/{memberId}")
    public Result<Void> addMemberFile(MultipartFile file,@PathVariable Long memberId) throws Exception {
        String url = UUID.randomUUID().toString();
        MemberFile memberFile = new MemberFile();
        memberFile.setMemberId(memberId);
        memberFile.setUrl(url);
        if (!memberFileService.save(memberFile)){
            log.error("添加文件失败");
            return Result.error("添加文件失败");
        }
        this.minioService.uploadFile(url, file.getInputStream(), file.getSize());
        log.info("添加文件成功");
        return Result.ok();
    }

    @SneakyThrows
    @DeleteMapping("/{memberId}")
    public Result<Void> deleteMemberFile(@PathVariable Long memberId) throws IOException {
        MemberFileVO memberFileVO = this.memberFileService.getByMemberId(memberId);
        if (memberFileVO == null){
            log.error("未查询到该用户的文件信息");
            return Result.error("未查询到该用户的文件信息");
        }
        for (String url : memberFileVO.getFileUrls()) {
            this.minioService.deleteFile(url);
        }
        this.memberFileService.lambdaUpdate().eq(MemberFile::getMemberId, memberId).remove();
        return Result.ok();
    }

    @DeleteMapping("/deleteByUrl/{url}")
    public Result<Void> deleteMemberFileByUrl(@PathVariable String url) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        this.minioService.deleteFile(url);
        this.memberFileService.lambdaUpdate().eq(MemberFile::getUrl, url).remove();
        return Result.ok();
    }

    @GetMapping("/downloadByUrl")
    public InputStream downloadFileByUrl(@RequestParam String url) throws Exception {
        return this.minioService.downloadFile(url);
    }
}
